#ifdef COMPILE

        #define _USE_GNU_C
        #include <mcs51/8052.h>

#else
typedef unsigned char _bool;
typedef unsigned char _u8;
typedef _bool         __bit;
typedef _bool         __sbit;
typedef _u8           __sfr;
        #define __at(v)

        #define __data
        #define __bdata
        #define __idata
        #define __pdata
        #define __xdata
        #define __code

        #define __interrupt(x)
#endif

/* STD KELI 关键字*/
#define _USE_STD_KELI_C
typedef __bit          bit;
typedef __sbit         sbit;
typedef __sfr          sfr;
typedef unsigned short Sfr16;
typedef unsigned short sfr16;
#define code __code

#include <mcs51/8052.h>

void
uart_init(unsigned char baud)
{
        TMOD |= 0x20;
        SCON = 0x50;
        PCON = 0x80;
        TH1  = baud;
        TL1  = baud;
        ES   = 1;
        EA   = 1;
        TR1  = 1;
}

unsigned char interrupt_times = 1;

void
main(void)
{
        uart_init(0xfa);

        while (1) {
                P2 = ~interrupt_times;
        }
}

unsigned char index           = 0;
unsigned char datas[32];
unsigned char state = 0;
unsigned char byte;

void
datas_handler_cb()
{
        for (index = 0; datas[index] != '\0'; ++index) {
                SBUF = datas[index];
                while (!TI)
                        ;
                TI = 0;
        }
        interrupt_times += 1;
}

void
uart_interrupt() __interrupt(4)
{
        // 清除中断标志位
        RI = 0;

        if (state == 0) {
                datas[index++] = SBUF;
                state          = 1;
        } else if (state == 1) {
                byte = SBUF;
                if (byte == '\r')
                        state = 2;
                else
                        datas[index++] = byte;
        } else if (state == 2) {
                byte = SBUF;
                if (byte == '\n') {
                        datas[index] = '\0';
                } else {
                        datas[0] = 'E';
                        datas[1] = 'R';
                        datas[2] = 'R';
                        datas[3] = '\0';
                }
                state = 0;
                index = 0;
                datas_handler_cb();
        }
        byte = SBUF;
}
